%**************************************************************
% Calculates the velocity
% Autor: Carlos Amaral (ceamaral@hotmail.com)
% data: 15/10/2010
% functions that are called: 
%**************************************************************

function velocity_general(measurement_list,data_type,data_size,variable,distance_between_sensors,freq_aq,tube_diameter,time)
% measurement_list is the name of the TXT file with the list of measurements to be
% analyzed. For example: timeseries_hist_PSD('text_name.txt',...
% If measurement_list is 'ALL', processes all measurements
% data_type can be: 'US', analysis of ultrasound
%                   'WM', analysis of wire-mesh
% data_size can be: 'RAW_DATA', analysis of syncronization results
%                   'SYNC_DATA', analysis of syncronization results
% variable can be:  void
%                   midle_electrode_void
%                   waterlevel (however graphic is wrong)
% freq_aq in Hz
% tube_diameter in meters

close all %Closes all Figures already open

if strcmp(data_size,'SYNC_DATA')         
    input_file_aux = 'SYNC';
    data_type_save = 'SYNC'; %used only to help saving files in case of selection
else
    input_file_aux = data_type;
    data_type_save = ''; %used only to help saving files in case of selection
end

% creates list of files that will be processed
if strcmp(measurement_list,'ALL')
    file_path_input = ['.\Matlab_data\*_' input_file_aux '.mat'];  %loads a list with the files to be processed
    files_list = dir(file_path_input);   
else
    [files_list] = txt_list_to_struct(measurement_list,input_file_aux,'.mat');
    data_type_save = [data_type_save '_selection']; %used only to help saving files in case of selection
end
 

num_files = length(files_list);

    if num_files > 0           %If there are wire-mesh measurements
        fprintf('     Velocity analysis from  "%s" data, variable: "%s".\n',data_type,variable);      
               
        for i=1:num_files
            fprintf('\    Processing %s\num_files', files_list(i).name);
            filename=['.\Matlab_data\' files_list(i).name];
            if(strcmp(data_type,'WM'))
                if(strcmp(variable,'void'))
                    filevariables = {'void_WM2', 'void_WM1'};            
                elseif(strcmp(variable,'waterlevel'))
                    filevariables = {'waterlevel_WM2', 'waterlevel_WM1'};
                elseif(strcmp(variable,'midle_electrode_void'))
                    filevariables = {'midle_electrode_void_WM1', 'midle_electrode_void_WM2'};
                end
            end
            
            if(strcmp(data_type,'US'))
                if(strcmp(variable,'void'))
                    filevariables = {'void_US2', 'void_US1'};

                elseif(strcmp(variable,'waterlevel'))
                    filevariables = {'waterlevel_US2', 'waterlevel_US1'};
                end
            end
            
            
            
             load(filename, filevariables{:});
%             name_workspace = ['.\Matlab_data\' files_list(i).name];
%             aux_load = load(name_workspace,variable);
%             void = getfield(aux_load,filevariables);         %#ok<GFLD>
            if strcmp(data_size,'SYNC_DATA') || strcmp(data_type,'US')    
                aux_name = ['.\Results_lvm\workspace_J_mean_average_US.mat'];    
            else    
                aux_name = ['.\Results_lvm\workspace_J_mean_average_WM.mat']; 
            end            
            
            if strcmp(data_size,'SYNC_DATA')    
                aux = ['_SYNC.mat'];    
            else    
                aux = ['_' data_type '.mat']; 
            end    
            
            lvmfilename = strrep(files_list(i).name,aux,'.lvm');   
            aux_load = load(aux_name,'num_seconds');
            time = getfield(aux_load,'num_seconds');         %#ok<GFLD>
            
            [JL_aux JG_aux]=lvm(lvmfilename,time);           
            
            if(strcmp(data_type,'WM'))
                if(strcmp(variable,'void'))
                    [matrix_velocity(i,:), velocity_corr_bubble_nose] = velocity_analysis(void_WM1,void_WM2,freq_aq,distance_between_sensors,JL_aux,JG_aux,tube_diameter); %Calculates the velocity        
                elseif(strcmp(variable,'waterlevel'))
                    [matrix_velocity(i,:), velocity_corr_bubble_nose] = velocity_analysis(waterlevel_WM1,waterlevel_WM2,freq_aq,distance_between_sensors,JL_aux,JG_aux,tube_diameter); %Calculates the velocity        
                elseif(strcmp(variable,'midle_electrode_void'))
                    [matrix_velocity(i,:), velocity_corr_bubble_nose] = velocity_analysis(midle_electrode_void_WM1,midle_electrode_void_WM2,freq_aq,distance_between_sensors,JL_aux,JG_aux,tube_diameter); %Calculates the velocity        
                end                       
            end
            
            if(strcmp(data_type,'US'))
                if(strcmp(variable,'void'))
                    [matrix_velocity(i,:), velocity_corr_bubble_nose] = velocity_analysis(void_US2,void_US1,freq_aq,distance_between_sensors,JL_aux,JG_aux,tube_diameter); %Calculates the velocity        
                elseif(strcmp(variable,'waterlevel'))
                    [matrix_velocity(i,:), velocity_corr_bubble_nose] = velocity_analysis(waterlevel_US2,waterlevel_US1,freq_aq,distance_between_sensors,JL_aux,JG_aux,tube_diameter); %Calculates the velocity        
                end
            end            
            
            if (exist('.\Results_velocity','dir') == 0)            %Creates Result directory if it does not exist yeat
                mkdir('Results_velocity');  
            end
             
             dir_variable = ['.\Results_velocity\' variable];
             if (exist(dir_variable,'dir') == 0)            %Creates Result directory if it does not exist yeat
                mkdir(dir_variable);  
             end

            name_file = files_list(i).name;
%             name_file = [dir '\Velocity_nose_correlation_WM_' name_file];         
%             columnHeader = {'JL','JG','velocities nose with correlation(m/s)'};         
%             xlswrite(name_file, columnHeader,'Sheet1','A1');
%             xlswrite(name_file, velocity_corr_bubble_nose,'Sheet1','A2');      

            % \TODO ??
            save_name = strrep(files_list(i).name,'.mat','');   
            savedir=[dir_variable '\velocity_' data_type '_' save_name '.mat'];
            save(savedir)           %Saves the workspace after velocity treatment                 
         
         
            name_figure_fig = [dir_variable '\Figure_' data_type '_velocity_' save_name '.fig'];
            name_figure_emf = [dir_variable '\Figure_' data_type '_velocity_' save_name '.tif'];
            saveas(gcf,name_figure_fig); 
            saveas(gcf,name_figure_emf);                  
                      
        end    
        %end of the loop, plot all results!
        plotVelocityResult(matrix_velocity,data_type,dir_variable,data_size);
    end
    
    return

end